package cn.green.core.components.store.field;

import cn.green.core.components.exceptions.DefaultTypeNullException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.green.core.components.field.DefaultTypeCom;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className DefaultValueTypeStore
 * @description 默认值类型仓库
 * @date 2024/11/18 9:04
 */
public class DefaultValueTypeStore {
    private static final Map<String, DefaultTypeCom> DEFAULT_VALUE_TYPE_STORE_MAP = new HashMap<>();
    private static final JSONArray OPTIONS = new JSONArray();

    public static void put(DefaultTypeCom store) {
        DEFAULT_VALUE_TYPE_STORE_MAP.put(store.typeCode(), store);
        JSONObject option = new JSONObject();
        option.put("label", store.typeName());
        option.put("value", store.typeCode());
        OPTIONS.add(option);
    }

    public static DefaultTypeCom get(String typeCode) {
        if (!DEFAULT_VALUE_TYPE_STORE_MAP.containsKey(typeCode)) {
            try {
                throw new DefaultTypeNullException(typeCode);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

        }
        return DEFAULT_VALUE_TYPE_STORE_MAP.get(typeCode);
    }

    public static JSONArray options() {
        return OPTIONS;
    }

}
